// pages/category/category.js
var template = require('../../template/template.js');
var request = require('../../utils/https.js')
var uri = 'index/deal_list.html'
Page({

  /**
   * 页面的初始数据
   */
  data: {
     goodsList:[],
     hideBottom:true,
     page:1,
     pageSize:6,
     loadingText:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    template.tabbar("tabBar", 1, this)//0表示第一个tabbar
    wx.showNavigationBarLoading();
    setTimeout(function(){
      wx.hideNavigationBarLoading();
    },2000)
  },
  getList: function () {
    var that = this;
    console.log(that);
    var pageIndex = that.data.page;
    request.req(uri, {
      user_id: wx.getStorageSync('userId'),
      page: that.data.page, page_size: that.data.pageSize
    },
      (err, res) => {
        if (res.data.status) {
          var list = [];
          if (res.data.data != '没有数据') {
            for (var i = 0; i < res.data.data.length; i++) {
              list.push(res.data.data[i]);
            }
          }else{
            that.setData({
              hideBottom: false,
              loadingText:'已无更多数据'
            })
            setTimeout(function(){
              that.setData({
                hideBottom: true,
                loadingText: '加载中...'
              })
            },2000)
          }
          if (that.data.page == 1) {
            that.setData({
              allPages: res.data.count,
              goodsList: list,
              hideBottom: true,
            })
          } else {
            var nowList = that.data.goodsList;
            nowList = nowList.concat(list);
            that.setData({
              allPages: res.data.count,
              goodsList: nowList,
              hideBottom: false,
              loadingText: '加载中...',
            })
            setTimeout(function () {
              that.setData({
                hideBottom: true,
                loadingText: '加载中...'
              })
            }, 2000)
          }
        }
      })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getList();
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    var that=this;
    this.setData({
      page:1
    })
    this.getList();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    this.setData({
      page: that.data.page+1
    })
    this.getList();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})